//microcommand define teha
inject(["puppeteer", "lnnbotSideloadFonts"])
option("font", "-f <family>", { fallback: "NotoSansCJKSC" })
signature("<text:rawtext>")
before(({ session }, text) => {
  if (!text) return session.i18n("internal.insufficient-arguments")
})
action(async ({ options }, text) => ctx.puppeteer.render(
  await ctx.http.get("https://vudrux.site/res/teha.svg", { responseType: "text" }),
  async (page, next) => {
    await page.evaluate(/*js*/ `
      const s = document.querySelector("svg")
      const style = document.createElementNS("http://www.w3.org/2000/svg", "style")
      style.textContent = ${JSON.stringify(await ctx.lnnbotSideloadFonts.getDeclarations())}
      s.appendChild(style)
      const t = document.createElementNS("http://www.w3.org/2000/svg", "text")
      t.setAttribute("x", "50%")
      t.setAttribute("y", "473")
      t.style.cssText = "text-anchor: middle; dominant-baseline: middle; baseline-shift: -0.1em;"
      t.setAttribute("font-size", "80")
      t.setAttribute("font-family", ${JSON.stringify(options.font)})
      t.textContent = ${JSON.stringify(text)}
      s.appendChild(t)
    `)
    await page.waitForNetworkIdle()
    await page.evaluate(/*js*/ `
      if (t.getBoundingClientRect().width > 480)
        for (let i = 0, b = 80, s = 0; i < 10; i++) {
          const m = (b + s) / 2
          t.setAttribute("font-size", m)
          if (t.getBoundingClientRect().width > 480) b = m
          else s = m
        }
    `)
    return next(await page.$("svg"))
  },
))
locale("zh-CN", {
  description: "“这也テ哈人了”生成器",
  options: {
    font: "指定字体族",
  },
})